<!--
 * @Author: qiye
 * @LastEditors: qiye
 * @description: page description
 * @Date: 2023-12-04 13:48:13
 * @LastEditTime: 2023-12-07 19:37:25
-->
<script lang="ts" setup>
import { IkSchedule } from 'iking-web-ui'
import { ref } from 'vue'
import { api } from '@/MainApp/apis/schedule'

const { msgSuccess, msgError } = useMessage()
const scheduleRef = ref<any>(null)
const events = ref<any>([])

const search = async () => {
  const res = await api.select()
  if (!res.success) {
    msgError(res.msg)
    return
  }
  for (const item of res.data) {
    item.start = item.estimateStartTime
    item.end = item.estimateEndTime
  }
  events.value = res.data

  console.log('events ', events.value)
}

const buildParams = (event: any, operation: string = 'add') => {
  const params: any = {
    title: event.title,
    remark: event.extendedProps?.remark,
    estimateStartTime: event.start,
    estimateEndTime: event.end,
    allDay: event.allDay
  }

  if (params.allDay) {
    params.estimateStartTime = `${params.estimateStartTime} 00:00:00`
    params.estimateEndTime = `${params.estimateEndTime} 00:00:00`
  }

  if (operation === 'update') {
    //
    params.id = event.id
  }
  return params
}

const handleAdd = async (event: any) => {
  const params = buildParams(event)
  const res = await api.add(params)
  if (!res.success) {
    msgError(res.msg)
    return
  }

  msgSuccess(res.msg)
  search()
}
const handleUpdate = async (event: any) => {
  const params = buildParams(event, 'update')
  const res = await api.update(params)
  if (!res.success) {
    msgError(res.msg)
    return
  }

  msgSuccess(res.msg)
  search()
}

const handleDelete = async (id: string) => {
  const res = await api.del(id)
  if (!res.success) {
    msgError(res.msg)
    return
  }
  msgSuccess(res.msg)
  search()
}

onMounted(() => {
  search()
})
</script>

<template>
  <IkPageMain fixed>
    <div class="schedule">
      <div class="schedule-content">
        <IkSchedule
          ref="scheduleRef"
          v-model="events"
          :removable="false"
          class="ik-schedule"
          @submitAdd="handleAdd"
          @submitUpdate="handleUpdate"
          @submitDelete="handleDelete"
        />
      </div>
    </div>
  </IkPageMain>
</template>

<style lang="scss" scoped>
.schedule{
  display: flex;
  box-sizing: border-box;
  overflow-y: auto;

  .schedule-content{
    flex-grow: 1;
    padding: 16px;

    .ik-schedule {
      // height: 1000px;
      // width: 100%;
    }
  }
}
</style>
